<?php

namespace app\api\controller;

use app\common\traits\ApiTrait;
use support\Request;
use support\Response;
use think\facade\Db;

class ChannelController
{
    use ApiTrait;

    /**
     * 不需要登录的方法
     */
    protected $noNeedLogin = ['login', 'activityList'];

    /**
     * 登录
     * @param Request $request
     * @return Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function login(Request $request) : Response
    {
        if (!request()->isPost()) {
            return $this->reError('请求方式错误');
        }

        $account = $request->post('account');
        $password = $request->post('password');
        $channel = Db::name('channel')->where(['account' => $account, 'password' => md5($password)])->find();
        if (!empty($channel)) {
            if ($channel['status'] == 1) {
                return $this->reSuccess('登录成功', ['id' => $channel['id']]);
            } else {
                return $this->reError('该账号已冻结');
            }
        } else {
            return $this->reError('用户名或密码错误');
        }
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function activityList(Request $request) : Response
    {
        if (!request()->isGet()) {
            return $this->reError('请求方式错误');
        }

        $channel_id = $request->get('channel_id');
        $page = $request->get('page');
        $limit = $request->get('limit');

        $channel = Db::name('channel')->where(['id' => $channel_id])->find();
        if (empty($channel)) {
            return $this->reError('该渠道不存在');
        }
        if ($channel['status'] == 0) {
            return $this->reError('该账号已冻结');
        }

        $list = Db::name('channel_activity')
            ->alias('b')
            ->join('activity c', 'b.activity_id = c.id')
            ->join('user u', 'c.user_id = u.id', 'left')
            ->field('c.id, c.activity_title, u.nickname')
            ->where(['b.channel_id' => $channel_id])
            ->order('b.channel_id desc')->page($page, $limit)->select()->toArray();
        foreach ($list as &$value) {
            if (empty($value['nickname'])) {
                $value['nickname'] = '';
            }
            $data = Db::name('order')
                ->alias('b')
                ->join('payment_disburse c', 'b.activity_id = c.activity_id and c.status = 1', 'left')
                ->field('sum(b.total_price) as order_total_price, count(b.id) as order_count')
                ->where(['b.activity_id' => $value['id'], 'b.order_status' => 2])
                ->find();
            $data['order_total_price'] = empty($data['order_total_price']) ? 0 : $data['order_total_price'];
            $data['order_count'] = empty($data['order_count']) ? 0 : $data['order_count'];
            $value = array_merge($value, $data);
        }
        return $this->reSuccess('数据获取成功', $list);
    }

}
